﻿using ECommon.Components;
using ESTAR.Infrastructure;
using System;
using System.Collections.Generic;
using System.Text;

namespace ESTAR.Domain.Auth
{
    /// <summary>
    /// 注册用户的服务
    /// </summary>
    [Component]
    public class RegisterUserInfoService
    {

        private readonly IUserInfoIndexRepository _userIndexRepository;

        public RegisterUserInfoService(IUserInfoIndexRepository userIndexRepository)
        {
            _userIndexRepository = userIndexRepository;
        }

        /// <summary>注册账号
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public void RegisterAccount(UserInfo user)
        {
            var accountIndex = _userIndexRepository.FindByUserName(user.UserName);
            if (accountIndex == null)
            {
                _userIndexRepository.Add(new UserInfoIndex(user.Id, user.UserName));
            }
            else if (accountIndex.UserId != user.Id)
            {
                throw new DuplicateAccountException(user.UserName);
            }
        }
    }
}
